﻿using KINLO.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Sql;
using System.Data.SqlClient;

namespace KINLO.Data.Reporitory
{
    public class ConfigReporitory
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(WeightReporitory));
        Database DB;
        public ConfigReporitory()
        {
            DB = (new DbContext("name=WeightDB")).Database;
        }
        public List<ParamConfig> GetConfigs()
        {
            var cmd = "SELECT ID ,CfgName ,CfgValue ,CfgValueType ,Remark FROM dbo.Config";
            return DB.SqlQuery<ParamConfig>(cmd).ToList();
        }
        public int UpdateParamConfig(ParamConfig param)
        {
            var cmd = @"UPDATE dbo.Config
                        SET CfgValue=@CfgValue,Remark=@Remark
                        WHERE ID=@ID";
            return DB.ExecuteSqlCommand(cmd,
                 new SqlParameter("@CfgValue", param.CfgValue),
                 new SqlParameter("@Remark", param.Remark)
                 );
        }
        public ParaConfig GetSettings()
        {
            try {
                var cmd = @"SELECT  * FROM dbo.ParaConfig";
                return DB.SqlQuery<ParaConfig>(cmd).ToList().FirstOrDefault();
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "ConfigReporitory", "GetSettings", ex));
                return null;
            }
        }

        public int UpdateSettiings(ParaConfig cfg)
        {
            try
            {
                var cmd = @"UPDATE dbo.ParaConfig
                        SET UITitle=@UITitle,Line=@Line,SerialNum=@SerialNum,LogSaveDays=@LogSaveDays,UnQualifiedShowMsg=@UnQualifiedShowMsg,
                        TestElecBalance=@TestElecBalance,PrintWeightLog=@PrintWeightLog,NotFrontTrayAsPass=@NotFrontTrayAsPass,
                        WeightRef1=@WeightRef1,WeightWave1=@WeightWave1,WeightRef2=@WeightRef2,WeightWave2=@WeightWave2,WeightRef3=@WeightRef3,
                        WeightWave3=@WeightWave3,EnableFrontInjectCheck=@EnableFrontInjectCheck,EnableBackInjectCheck=@EnableBackInjectCheck,
                        RecordSaveDays=@RecordSaveDays,WeightTimeout=@WeightTimeout,FWeightEmptyLow=@FWeightEmptyLow,WeightWave1Min=@WeightWave1Min,
                        WeightWave1Max=@WeightWave1Max,InjValueEmptyLow=@InjValueEmptyLow WHERE ID=@ID";
                return DB.ExecuteSqlCommand(cmd,
                    new SqlParameter("@ID", cfg.ID),
                    new SqlParameter("@UITitle", cfg.UITitle ?? ""),
                    new SqlParameter("@Line", cfg.Line ?? ""),
                    new SqlParameter("@SerialNum", cfg.SerialNum),
                    new SqlParameter("@LogSaveDays", cfg.LogSaveDays),
                    new SqlParameter("@UnQualifiedShowMsg", cfg.UnQualifiedShowMsg ),
                    new SqlParameter("@TestElecBalance", cfg.TestElecBalance ),
                    new SqlParameter("@PrintWeightLog", cfg.PrintWeightLog),
                    new SqlParameter("@NotFrontTrayAsPass", cfg.NotFrontTrayAsPass),
                    new SqlParameter("@WeightRef1", cfg.WeightRef1),
                    new SqlParameter("@WeightWave1", cfg.WeightWave1),
                    new SqlParameter("@WeightRef2", cfg.WeightRef2),
                    new SqlParameter("@WeightWave2", cfg.WeightWave2),
                    new SqlParameter("@WeightRef3", cfg.WeightRef3),
                    new SqlParameter("@WeightWave3", cfg.WeightWave3),
                    new SqlParameter("@EnableFrontInjectCheck", cfg.EnableFrontInjectCheck),
                    new SqlParameter("@EnableBackInjectCheck", cfg.EnableBackInjectCheck),
                    new SqlParameter("@RecordSaveDays", cfg.RecordSaveDays),
                    new SqlParameter("@WeightTimeout", cfg.WeightTimeout),
                    new SqlParameter("@FWeightEmptyLow", cfg.FWeightEmptyLow),
                    new SqlParameter("@WeightWave1Min", cfg.WeightWave1Min),
                    new SqlParameter("@WeightWave1Max", cfg.WeightWave1Max),
                    new SqlParameter("@InjValueEmptyLow", cfg.InjValueEmptyLow?? 0)             
                    );
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "ConfigReporitory", "UpdateSettiings", ex));
                return 0;
            } 
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        public int UpdatePass(string pass)
        {
            var cmd = @"UPDATE dbo.ParaConfig SET FrontWeightPass=@FrontWeightPass";
           return  DB.ExecuteSqlCommand(cmd,
                new SqlParameter("@FrontWeightPass", pass));
        }

    }
}
